package com.boxin.ims.modules.sys.scheduler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.bi.dao.BIDao;
import com.boxin.ims.modules.bi.entity.Series;
import com.boxin.ims.modules.bi.entity.Tooltip;
import com.boxin.ims.modules.bi.utils.DateProcessingUtils;
import com.boxin.ims.modules.wechat.entity.WechatScene;
import com.boxin.ims.modules.wechat.service.WechatSceneService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

@Service
public class BiMemberMessageProcessScheduler  extends AbstractScheduler {
	String month=DateUtils.getMonthString();
	String year=DateUtils.getYearString();
	
	@Autowired
	 BIDao biDao;
	
	@Autowired
	private WechatSceneService wechatSceneService;
	
	
	public BiMemberMessageProcessScheduler() {
		super();
	}
	
	@Override
	public String frequency() {
		return this.getSchedulerInfo().getFrequence();
	}

	@Override
	public void process() {
		
		
		Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();


		 SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");
		String createTime=sp.format(d);//获取昨天日期
		createTime=createTime.replace("-", "");
		
		year=createTime.substring(0, 4);
		month=createTime.substring(4, 6);
		
		
		updateBiDataResult(month, year);
		
		if("01".equals(com.thinkgem.jeesite.common.utils.DateUtils.getDay())){
			month=DateUtils.getMonthString();
			year=DateUtils.getYearString();
			
			updateBiDataResult(month, year);
			
			biDao.deleteWechatMessage();
			
		}
		
		
	}
	
	
	public void updateBiDataResult(String month,String year){
		
		
		
		
		Map<String,Object> params1 =new HashMap<String ,Object>();
		
		
		
		List<User> userList= UserUtils.findAllList();
		
		
		for (int l = 0;l < userList.size();l++) {
			String str=biMember(userList.get(l));
			params1.put("month", month);
			params1.put("user_id",userList.get(l).getId());
			params1.put("bi_type",1);
			List<Map<String, Object>> biDataResultList = biDao.queryBiDataResult(params1);
			Boolean flag=biDataResultList.size()==0;
			if(flag){
				Map< String, Object> params=new HashMap<String ,Object>(); 
				params.put("user_id", userList.get(l).getId());
				params.put("bi_type", 1);
				params.put("result", str);
				params.put("year", year);
				params.put("month", month);
				params.put("create_time",  new   Date(System.currentTimeMillis()));
				biDao.addBiDataResult(params);
			}else{
				for (int i = 0; i < biDataResultList.size(); i++) {
					if(biDataResultList.get(i).get("user_id").toString().equals(userList.get(l).getId()+"")){
						Map<String, Object> params=new HashMap<String, Object>();
						params.put("result", str);
						params.put("id", biDataResultList.get(i).get("id"));
						biDao.updateBIDataResultById(params);
					}
				}
			}
			
			
			/**
			 * 会员注册人数分析
			 */
			params1.put("month", month);
			params1.put("user_id",userList.get(l).getId());
			params1.put("bi_type",2);
			biDataResultList = biDao.queryBiDataResult(params1);
			flag=biDataResultList.size()==0;
			String str1=biWechatMemberList(userList.get(l));
			if(flag){
				Map< String, Object> params=new HashMap<String ,Object>(); 
				params.put("user_id", userList.get(l).getId());
				params.put("bi_type", 2);
				params.put("result", str1);
				params.put("year", year);
				params.put("month", month);
				params.put("create_time",  new   Date(System.currentTimeMillis()));
				biDao.addBiDataResult(params);
			}else{
				for (int i = 0; i < biDataResultList.size(); i++) {
					if(biDataResultList.get(i).get("user_id").toString().equals(userList.get(l).getId()+"")){
						Map<String, Object> params=new HashMap<String, Object>();
						params.put("result", str1);
						params.put("id", biDataResultList.get(i).get("id"));
						biDao.updateBIDataResultById(params);
					}
				}
			}
			
			/**
			 * 平台活跃人数
			 */
			params1.put("month", month);
			params1.put("user_id",userList.get(l).getId());
			params1.put("bi_type",5);
			biDataResultList = biDao.queryBiDataResult(params1);
			flag=biDataResultList.size()==0;
			String str5=biAliveMemberList(userList.get(l));
			System.err.println(str5);
			if(flag){
				Map< String, Object> params=new HashMap<String ,Object>(); 
				params.put("user_id", userList.get(l).getId());
				params.put("bi_type", 5);
				params.put("result", str5);
				params.put("year", year);
				params.put("month", month);
				params.put("create_time",  new   Date(System.currentTimeMillis()));
				biDao.addBiDataResult(params);
			}else{
				for (int i = 0; i < biDataResultList.size(); i++) {
					if(biDataResultList.get(i).get("user_id").toString().equals(userList.get(l).getId()+"")){
						Map<String, Object> params=new HashMap<String, Object>();
						params.put("result", str5);
						params.put("id", biDataResultList.get(i).get("id"));
						biDao.updateBIDataResultById(params);
					}
				}
			}
			
			
			/**
			 * 二维码取到关注分析
			 */
			params1.put("month", month);
			params1.put("user_id",userList.get(l).getId());
			params1.put("bi_type",3);
			biDataResultList = biDao.queryBiDataResult(params1);
			flag=biDataResultList.size()==0;
			String str3=qrCodeSubscribeMessageByDay(userList.get(l));
			if(flag){
				Map< String, Object> params=new HashMap<String ,Object>(); 
				params.put("user_id", userList.get(l).getId());
				params.put("bi_type",3);
				params.put("result", str3);
				params.put("year", year);
				params.put("month", month);
				params.put("create_time",  new   Date(System.currentTimeMillis()));
				biDao.addBiDataResult(params);
			}else{
				for (int i = 0; i < biDataResultList.size(); i++) {
					if(biDataResultList.get(i).get("user_id").toString().equals(userList.get(l).getId()+"")){
						Map<String, Object> params=new HashMap<String, Object>();
						params.put("result", str3);
						params.put("id", biDataResultList.get(i).get("id"));
						biDao.updateBIDataResultById(params);
					}
				}
			}
			
			/**
			 * 会员消息分析
			 */
			params1.put("month", month);
			params1.put("user_id",userList.get(l).getId());
			params1.put("bi_type",4);
			biDataResultList = biDao.queryBiDataResult(params1);
			flag=biDataResultList.size()==0;
			String str4=wechatReceiveList(userList.get(l));
			if(flag){
				Map< String, Object> params=new HashMap<String ,Object>(); 
				params.put("user_id", userList.get(l).getId());
				params.put("bi_type",4);
				params.put("result", str4);
				params.put("year", year);
				params.put("month", month);
				params.put("create_time",  new   Date(System.currentTimeMillis()));
				biDao.addBiDataResult(params);
			}else{
				for (int i = 0; i < biDataResultList.size(); i++) {
					if(biDataResultList.get(i).get("user_id").toString().equals(userList.get(l).getId()+"")){
						Map<String, Object> params=new HashMap<String, Object>();
						params.put("result", str4);
						params.put("id", biDataResultList.get(i).get("id"));
						biDao.updateBIDataResultById(params);
					}
				}
			}
			
			
			/**
			 *  用户充值统计
			 */
			
			
			
		}
	}
	
	
	public String sceneProcess(String message){
		return message.substring(message.indexOf("qrscene_")+8, message.lastIndexOf("Ticket"));
	}
	
	/**
	 * 用户关注数分析
	 *@author xiaoqian
	 * @param user
	 * @return
	 * 2014-5-19下午4:03:36
	 */
	public String biMember(User user){
		StringBuffer stringBuffer = new StringBuffer();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("user_id",user.getId());
		params.put("month", month);  
		params.put("year", year);  
		
		params.put("event_type", "subscribe");
		List<Map<String,Object>> subscribeList=biDao.queryMessageStatInfo(params);
		
		
		List<Map<String,Object>> qrCodeSubscribeList=biDao.querySubscribeMessage(params);
		
		params.put("event_type", "unsubscribe");
		List<Map<String,Object>> unsubscribeList=biDao.queryMessageStatInfo(params);
		
		
		
		if(month==null||year==null||Integer.parseInt(month)<0||Integer.parseInt(month)>13||year==""||month==""){
		}else{
			
			String[] ints =  DateProcessingUtils.daysMountBymonth(month,year);
			
			
			Integer[] data4=DateProcessingUtils.getData(ints, qrCodeSubscribeList);
			Integer[] data2=DateProcessingUtils.getData(ints, subscribeList);
			Integer[] data3=DateProcessingUtils.getData(ints, unsubscribeList);
			
			
			Series series2= new Series();
			series2.setName("订阅人数");
			series2.setType("spline");
			Tooltip tooltip2 = new Tooltip();
			tooltip2.setValueSuffix("人");
			series2.setTooltip(tooltip2);
			series2.setData(data2);
			series2.setPointStart(year,(Integer.parseInt(month)-1)+"" );
			
			Series series3= new Series();
			series3.setName("退订人数");
			series3.setType("spline");
			Tooltip tooltip3 = new Tooltip();
			tooltip3.setValueSuffix("人");
			series3.setTooltip(tooltip3);
			series3.setData(data3);
			series3.setPointStart(year, (Integer.parseInt(month)-1)+"");
			
			
			Series series4= new Series();
			series4.setName("通过二维码订阅的人数");
			series4.setType("spline");
			Tooltip tooltip4 = new Tooltip();
			tooltip4.setValueSuffix("人");
			series4.setTooltip(tooltip4);
			series4.setData(data4);
			series4.setPointStart(year, (Integer.parseInt(month)-1)+"");
			
			Integer[] data1=new Integer[ints.length];
			for (int i = 0; i < ints.length; i++) {
				data1[i]=data2[i]-data3[i];
			}
			Series series1= new Series();
			series1.setName("净增长人数");
			series1.setType("spline");
			Tooltip tooltip1 = new Tooltip();
			tooltip1.setValueSuffix("次");
			series1.setTooltip(tooltip1);
			series1.setData(data1);
			series1.setPointStart(year, (Integer.parseInt(month)-1)+"");
			
			
			stringBuffer.append("[")
			.append(DateProcessingUtils.getSeriesJson(series1)).append(",")
			.append(DateProcessingUtils.getSeriesJson(series2)).append(",")
			.append(DateProcessingUtils.getSeriesJson(series3)).append(",")
			.append(DateProcessingUtils.getSeriesJson(series4)).append(",")
			.append("]");
			
		}
		return stringBuffer.toString();
	}
	
	
	/**
	 * 会员注册人数分析
	 *@author xiaoqian
	 * @param user
	 * @return
	 * 2014-5-19下午4:04:06
	 */
	public String biWechatMemberList(User user){
		StringBuffer stringBuffer = new StringBuffer();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("user_id",user.getId());
		params.put("createTime",year+ month);  
		
		
		List<Map<String,Object>> biWechatMemberList=biDao.queryBiWechatMember(params);
		
		if(month==null||year==null||Integer.parseInt(month)<0||Integer.parseInt(month)>13||year==""||month==""){
		}else{
			
			String[] ints =  DateProcessingUtils.daysMountBymonth(month,year);
			
			
			Integer[] data6=DateProcessingUtils.getData(ints, biWechatMemberList);
			
			
			
			Series series6= new Series();
			series6.setName("会员注册人数");
			series6.setType("spline");
			Tooltip tooltip6 = new Tooltip();
			tooltip6.setValueSuffix("人");
			series6.setTooltip(tooltip6);
			series6.setData(data6);
			series6.setPointStart(year,(Integer.parseInt(month)-1)+"" );
			
			
			stringBuffer.append("[")
			.append(DateProcessingUtils.getSeriesJson(series6)).append(",")
			.append("]");
	}
		return stringBuffer.toString();
	}
	
	
	
	/**
	 * 平台活跃人数
	 *@author xiaoqian
	 * @param user
	 * @return
	 * 2014-5-19下午4:04:06
	 */
	public String biAliveMemberList(User user){
		StringBuffer stringBuffer = new StringBuffer();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("user_id",user.getId());
		params.put("month",month);  
		params.put("year",year);  
		
		List<Map<String,Object>> biWechatMemberList=biDao.queryBiAliveMember(params);
		
			if(month==null||year==null||Integer.parseInt(month)<0||Integer.parseInt(month)>13||year==""||month==""){
			}else{
				
				String[] ints =  DateProcessingUtils.daysMountBymonth(month,year);
				
				List<Map<String, Object>> data6=DateProcessingUtils.getDataAndLink(ints, biWechatMemberList,year+month);
				
				
				Series series6= new Series();
				series6.setName("平台活跃人数");
				series6.setType("column");
				series6.setData(data6);
				series6.setPointStart(year,(Integer.parseInt(month)-1)+"" );
				
				stringBuffer.append("[")
				.append(DateProcessingUtils.getSeriesJson(series6)).append(",")
				.append("]");
			}
		return stringBuffer.toString();
	}
	
	
	/**
	 * 二维码取到关注分析
	 *@author xiaoqian
	 * @param user
	 * @return
	 * 2014-5-19下午4:04:06
	 */
	public String qrCodeSubscribeMessageByDay(User user){
		StringBuffer stringBuffer = new StringBuffer();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("user_id",user.getId());
		params.put("month", year+month);  
		
		
		WechatScene wechatScene = new WechatScene();
		user.setId(user.getId());
		wechatScene.setUser(user);
		List<WechatScene>  list=wechatSceneService.find(wechatScene);
		
		List<List<Map<String,Object>>> sceneLists = new ArrayList<List<Map<String,Object>>>();
		
		for (int i = 0; i < list.size(); i++) {
			params.put("scene_id", list.get(i).getSceneId());
			
			List<Map<String,Object>> querySubscribeMessageByQrCode=biDao.querySceneMessageByDay(params);
			sceneLists.add(querySubscribeMessageByQrCode);
		}
		
		params.put("event_type", "subscribe");
		
		params.put("year",year);  
		params.put("month", month);  
		List<Map<String,Object>> querySubscribeMessageByQrCode=biDao.querySubscribeMessageByQrCode(params);
		
		
		if(month==null||year==null||Integer.parseInt(month)<0||Integer.parseInt(month)>13||year==""||month==""){
		}else{
			
			String[] ints =  DateProcessingUtils.daysMountBymonth(month,year);
			
			
			List<Series> seriesLists = new ArrayList<Series>();
			Integer[] data0=DateProcessingUtils.getData(ints, querySubscribeMessageByQrCode);
			for (int i =0; i < sceneLists.size(); i++) {
				Integer[] datai=DateProcessingUtils.getData(ints, sceneLists.get(i));
				Series seriesi= new Series();
				seriesi.setName(list.get(i).getScene());
				seriesi.setType("column");
				Tooltip tooltipi = new Tooltip();
				tooltipi.setValueSuffix("次");
				seriesi.setTooltip(tooltipi);
				seriesi.setData(datai);
				seriesi.setPointStart(year,(Integer.parseInt(month)-1)+"" );
				seriesLists.add(seriesi);
			}
			
			
			
			
			Series series0= new Series();
			series0.setName("二维码扫描渠道总数");
			series0.setType("spline");
			Tooltip tooltip0 = new Tooltip();
			tooltip0.setValueSuffix("次");
			series0.setTooltip(tooltip0);
			series0.setData(data0);
			series0.setPointStart(year, (Integer.parseInt(month)-1)+"");
			
			
			StringBuffer seriesStr = new StringBuffer();
			for (int i = 0; i < seriesLists.size(); i++) {
				seriesStr.append(DateProcessingUtils.getSeriesJson(seriesLists.get(i))).append(",");
			}
			
			
			stringBuffer.append("[")
			.append(DateProcessingUtils.getSeriesJson(series0)).append(",")
			.append(seriesStr)
			.append("]");
			
	}
		return stringBuffer.toString();
	}
	
	
	/**
	 *会员消息分析
	 *@author xiaoqian
	 * @param user
	 * @return
	 * 2014-5-19下午4:04:06
	 */
	public String wechatReceiveList(User user){
		StringBuffer stringBuffer = new StringBuffer();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("user_id",user.getId());
		params.put("month", month);  
		params.put("year", year);  
		
		
		List<Map<String,Object>> generalMessageList=biDao.queryGeneralMessage(params);
		
		params.put("message_type", "text");
		List<Map<String,Object>> textList=biDao.queryMessageStatInfo(params);
		
		params.put("message_type", "image");
		List<Map<String,Object>> imageList=biDao.queryMessageStatInfo(params);
		
		params.put("message_type", "voice");
		List<Map<String,Object>> voiceList=biDao.queryMessageStatInfo(params);
		
		params.put("message_type", "video");
		List<Map<String,Object>> videoList=biDao.queryMessageStatInfo(params);
		
		params.put("message_type", "link");
		List<Map<String,Object>> linkList=biDao.queryMessageStatInfo(params);
		
		if(month==null||year==null||Integer.parseInt(month)<0||Integer.parseInt(month)>13||year==""||month==""){
		}else{
			
			String[] ints =  DateProcessingUtils.daysMountBymonth(month,year);
			
			
			Integer[] data1=DateProcessingUtils.getData(ints, textList);
			Integer[] data2=DateProcessingUtils.getData(ints, imageList);
			Integer[] data3=DateProcessingUtils.getData(ints, voiceList);
			Integer[] data4=DateProcessingUtils.getData(ints, videoList);
			Integer[] data5=DateProcessingUtils.getData(ints, linkList);
			Integer[] data6=DateProcessingUtils.getData(ints, generalMessageList);
			
			
			
			
			Series series1= new Series();
			series1.setName("文本消息");
			series1.setType("spline");
			Tooltip tooltip1 = new Tooltip();
			tooltip1.setValueSuffix("条");
			series1.setTooltip(tooltip1);
			series1.setData(data1);
			series1.setPointStart(year,(Integer.parseInt(month)-1)+"" );
			
			Series series2= new Series();
			series2.setName("图片消息");
			series2.setType("spline");
			Tooltip tooltip2 = new Tooltip();
			tooltip2.setValueSuffix("条");
			series2.setTooltip(tooltip2);
			series2.setData(data2);
			series2.setPointStart(year,(Integer.parseInt(month)-1)+"" );
			
			Series series3= new Series();
			series3.setName("语音消息");
			series3.setType("spline");
			Tooltip tooltip3 = new Tooltip();
			tooltip3.setValueSuffix("条");
			series3.setTooltip(tooltip3);
			series3.setData(data3);
			series3.setPointStart(year, (Integer.parseInt(month)-1)+"");
			
			
			Series series4= new Series();
			series4.setName("视频消息");
			series4.setType("spline");
			Tooltip tooltip4 = new Tooltip();
			tooltip4.setValueSuffix("条");
			series4.setTooltip(tooltip4);
			series4.setData(data4);
			series4.setPointStart(year, (Integer.parseInt(month)-1)+"");
			
			Series series5= new Series();
			series5.setName("连接消息");
			series5.setType("spline");
			Tooltip tooltip5 = new Tooltip();
			tooltip5.setValueSuffix("条");
			series5.setTooltip(tooltip5);
			series5.setData(data5);
			series5.setPointStart(year,(Integer.parseInt(month)-1)+"" );
			
			
			Series series6= new Series();
			series6.setName("普通消息总数");
			series6.setType("spline");
			Tooltip tooltip6 = new Tooltip();
			tooltip6.setValueSuffix("条");
			series6.setTooltip(tooltip6);
			series6.setData(data6);
			series6.setPointStart(year,(Integer.parseInt(month)-1)+"" );
			
			
			
			
			
			stringBuffer.append("[")
			.append(DateProcessingUtils.getSeriesJson(series1)).append(",")
			.append(DateProcessingUtils.getSeriesJson(series2)).append(",")
			.append(DateProcessingUtils.getSeriesJson(series3)).append(",")
			.append(DateProcessingUtils.getSeriesJson(series4)).append(",")
			.append(DateProcessingUtils.getSeriesJson(series5)).append(",")
			.append(DateProcessingUtils.getSeriesJson(series6)).append(",")
			.append("]");
	}
		return stringBuffer.toString();
	}
	
	
	
	
	/**
	 *储值分析
	 *@author xiaoqian
	 * @param user
	 * @return
	 * 2014-5-19下午4:04:06
	 */
	public String biRechargeLogList(User user){
		StringBuffer stringBuffer = new StringBuffer();
		return "";
	}
}
